/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CategoriaDlg.java
 *
 * Created on May 3, 2009, 4:31:20 AM
 */
package dialogs;

import util.CategoriaNode;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Novo usuário
 */
public class CategoriaDlg extends javax.swing.JDialog {

    private DefaultMutableTreeNode catNode_insert;
    private MobCatTreeModel model;
    private CategoriaNode catNode_edit;

    /** Creates new form CategoriaDlg */
    public CategoriaDlg(java.awt.Frame parent, boolean modal, MobCatTreeModel model) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(parent);
        this.model = model;
    }

    public void showInserir(DefaultMutableTreeNode catPai) {
        this.catNode_insert =  catPai;
        txCatNome.setText("");
        txCatDesc.setText("");
        setTitle("Nova Categoria");
        super.show();
    }

     public void showEditar(CategoriaNode catNode) {
        this.catNode_edit = catNode;
        txCatNome.setText(catNode.getCategoria().getNome());
        txCatDesc.setText(catNode.getCategoria().getResumoFilhos());
        setTitle("Editar Categoria");
        super.show();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txCatNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnSalvarCat = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        txCatDesc = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mobcatadmin.MobCatAdminApp.class).getContext().getResourceMap(CategoriaDlg.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txCatNome.setName("txCatNome"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        btnSalvarCat.setText(resourceMap.getString("btnSalvarCat.text")); // NOI18N
        btnSalvarCat.setName("btnSalvarCat"); // NOI18N
        btnSalvarCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarCatActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        txCatDesc.setText(resourceMap.getString("txtCatDesc.text")); // NOI18N
        txCatDesc.setName("txtCatDesc"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txCatDesc, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txCatNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(btnSalvarCat)
                        .addGap(20, 20, 20)
                        .addComponent(jButton1, 0, 0, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txCatNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txCatDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvarCat)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarCatActionPerformed
        if (catNode_edit != null) {
            catNode_edit.getCategoria().setNome(txCatNome.getText());
            catNode_edit.getCategoria().setResumoFilhos(txCatDesc.getText());
            model.editarCategoria(catNode_edit);
        }else{
            model.criarCategoria(txCatNome.getText(), txCatDesc.getText(), catNode_insert);
        }
        dispose();
        setVisible(false);
}//GEN-LAST:event_btnSalvarCatActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
        this.setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvarCat;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txCatDesc;
    private javax.swing.JTextField txCatNome;
    // End of variables declaration//GEN-END:variables
}
